<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="添加处理单" width="70%" append-to-body>
      <el-scrollbar always="true" max-height="60vh">
        <div class="ui_el_dialog_body">
          <el-form ref="form" :model="formData" :rules="rules" label-width="80">
            <h3 class="from-title-style"> 基本信息</h3>
            <el-row>
              <el-col :span=" 8">
                <el-form-item label="发起时间" prop="startTime">
                  <date-editor v-model="formData.startTime" placeholder="发起时间" :clearable="true">
                  </date-editor>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="处理费用" prop="fee">
                  <el-input v-model="formData.fee" placeholder="请输入处理费用"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="处理类型" prop="handleType">
                  <el-select v-model="formData.handleType">
                    <el-option v-for="t in handleTypeList" :label="t.label" :value="t.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="处理原因" prop="remark">
                  <el-input type="textarea" v-model="formData.remark" placeholder="请输入处理原因"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <h3 class="from-title-style">资产列表</h3>
          <choose-table ref="chooseTable">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="分类" prop="name"></el-table-column>
            <el-table-column label="品牌" prop="brand"></el-table-column>
            <el-table-column label="型号" prop="model"></el-table-column>
          </choose-table>
        </div>
      </el-scrollbar>


      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>

      </div>
    </el-dialog>

  </div>
</template>

<script setup>

import ChooseTable from "@/components/Asset/ChooseTable.vue";
import api from "@/api/asset/apply.js";
import {handleTypeList} from "./data.js";
import DateEditor from "@/components/Common/DateEditor.vue";

const emit = defineEmits(['sub']);

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  },
  applyTypeName: {
    type: String,
    required: true,
  },
})

const chooseTable = ref();

// 是否编辑
const isEdit = ref(false);

const applyDataInit = {
  id: null,
  applyType: props.applyType,
  applyTypeName: props.applyTypeName,
  startTime: null
}

const applyData = reactive({...applyDataInit});

const formDataInit = {
  fee: null,
  handleType: null,
  remark: null
}

// 表单规则
const rules = {
  handleType: [
    {
      required: true,
      message: '处理类型不能为空',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 自定义校验：处理类型必须是已选择的有效项
        if (!handleTypeList.some(item => item.value === value)) {
          callback(new Error('请选择有效的处理类型'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: false,
      message: '备注不能为空',
      trigger: 'blur'
    },
    {
      max: 200,
      message: '备注不能超过200个字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 自定义校验：备注的内容不能仅包含空格
        if (value && !/\S/.test(value)) {
          callback(new Error('备注不能仅包含空格'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
}


const openFlag = ref(false);
const formData = reactive({...formDataInit})

function reset() {
  Object.assign(formData, formDataInit);
  // tableData.list = [];
}

function open(row) {
  openFlag.value = true;

  reset();
  if (row) {
    api.detail(row.id).then(response => {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, resFormData);
      }
      let relItems = formData.items;
      if (!relItems) {
        relItems = []
      }
      chooseTable.value.setList(relItems)
    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
}

function close() {
  openFlag.value = false;
}

function getParams() {
  const table = chooseTable.value;
  const apply = {...applyData};
  apply.applyType = props.applyType;
  formData.items = table.getList();
  apply.formData = JSON.stringify(formData);
  return {
    edit: isEdit.value,
    apply: apply
  }
}

/** 保存 */
function save() {

  api.save(getParams()).then(response => {
    emit("sub");
    close();
  });
}

function sub() {
  api.saveAndSub(getParams()).then(response => {
    emit("sub");
    close();
  });
}

defineExpose({
  open
})


</script>
